package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Bank;
import com.yami.shop.bean.model.Carry;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.PageParam;

import com.yami.shop.dao.BankMapper;
import com.yami.shop.dao.CarryMapper;
import com.yami.shop.dao.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("/admin/carry")
@Api(tags = "提现管理")
public class CarryController {

        @Autowired
        private  CarryMapper carryMapper;
        @Autowired
        private BankMapper bankMapper;
        @Autowired
        private UserMapper userMapper;

        @GetMapping("/queryCarry")
        @ApiOperation(value = "查询用户提现申请列表",notes = "查询用户提现申请列表")
        public ResponseEntity<IPage<Carry>> queryCarry(PageParam page,Integer state){
                IPage<Carry> carrys = carryMapper.selectPage(page,new QueryWrapper<Carry>()
                .eq(state != null,"state",state));
                for(Carry carry : carrys.getRecords()){
                        Bank ban = bankMapper.selectById(carry.getBankId());
                        carry.setBank(ban);
                        carry.setKaHao(ban.getBankCard());
                        carry.setYinHang(ban.getAffiliatedBank());
                        carry.setXingMing(ban.getRealName());
                }
               return ResponseEntity.ok(carrys);
        }

        @PostMapping("/editCarry")
        @ApiOperation(value = "处理用户提现",notes = "处理用户提现")
        public ResponseEntity<String> editCarry(@RequestBody Carry userCarry){
                User user = userMapper.selectById(userCarry.getUserId());
                Date now = new Date();
                userCarry.setHandleTime(now);
                if(userCarry.getState() == 2){
                        Double num = user.getWallet() + userCarry.getRmb();
                        user.setWallet(num);
                        userMapper.updateById(user);
                }
                int res = carryMapper.update(userCarry,new QueryWrapper<Carry>()
                        .eq("carry_id",userCarry.getCarryId()));
                return ResponseEntity.ok().build();
        }

}
